<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">商品管理</div></el-breadcrumb-item
        >
        <el-breadcrumb-item :to="{ path: '/addGoodsWareHouse' }"
          ><div class="div2">仓库库位</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <!-- 添加客户等级 -->
    <div class="content">
      <p class="header">添加库位</p>
      <el-form :inline="true" :model="formInline1" class="demo-form-inline">
        <el-form-item label="仓库库位">
          <el-input
            v-model="formInline1.name"
            placeholder="请输入库位名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="listAxios(1)">搜索</el-button>

          <el-button
            type="primary"
            @click="dialoVisible = true;formInline={};"
            v-if="parArray.indexOf('locationAddCode') != -1"
            >新增库位</el-button
          >
          <!-- onSubmit -->
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加客户等级 -->
    <!-- 管理客户等级 -->
    <div class="manage">
      <p class="header">管理分类</p>
      <div class="table">
        <el-table :data="tableData.list">
          <el-table-column
            label="序号"
            type="index"
            width="50px;"
          ></el-table-column>
          <el-table-column scope="scope" label="仓库名称" width="200">
            <template slot-scope="scope">
              <span>{{ scope.row.warName }}</span>
            </template>
          </el-table-column>

          <el-table-column scope="scope" label="库位名称" width="200">
            <template slot-scope="scope">
              <el-input
                v-if="scope.row.edit"
                v-model="scope.row.name"
              ></el-input>
              <span v-else>{{ scope.row.name }}</span>
            </template>
          </el-table-column>

          <el-table-column label="更新时间">
            <template slot-scope="scope">
              <span>{{ $moment(scope.row.createdTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="optain">
                <p
                  @click="scope.row.edit = !scope.row.edit"
                  v-if="!scope.row.edit && parArray.indexOf('locationEditCode') != -1"
                >
                  <span>编辑</span>
                </p>

                <p
                  @click="fixHouser(scope.row)"
                  v-if="scope.row.edit && parArray.indexOf('locationEditCode') != -1"
                >
                  <span>保存</span>
                </p>

                <p
                  @click="scope.row.edit = !scope.row.edit,scope.row.name = scope.row.name1,
              scope.row.warName = scope.row.warName1"
                  v-if="scope.row.edit && parArray.indexOf('locationEditCode') != -1"
                >
                  <span>取消</span>
                </p>

                <p
                  @click="delect(scope.row, scope.$index)"
                  v-if="parArray.indexOf('locationDelect') != -1"
                >
                  <span>删除</span>
                </p>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 管理客户等级 -->
    <!-- 弹框  -->
    <el-dialog
      :visible.sync="dialoVisible"
      :append-to-body="true"
      title="新增库位"
    >
      <div class="form">
        <el-form label-position="right" label-width="80px" :model="formInline">
          <el-form-item label="仓库名称">
            <el-select
              filterable
              size="small"
              clearable
              v-model="formInline.name"
              placeholder="请选择商品仓库"
            >
              <el-option :label="this.content.name" :value="this.content.name">
              </el-option>
              <el-option
                :label="this.content.damagedName"
                :value="this.content.damagedName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="库位名称">
            <el-input v-model="formInline.location" maxlength="30"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialoVisible = false"
            >取 消</el-button
          >
          <el-button size="small" type="primary" @click="onSubmit()"
            >确认</el-button
          >
        </div>
      </div>
    </el-dialog>
    <!-- 弹框  -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        name: "",
        location: "",
      },
      formInline1: {
        name: "",
      },
      content: "", //传递的参数
      tableData: [],
      limits: "",
      parArray: [],
      dialoVisible: false,
    };
  },
  created() {
    this.content = JSON.parse(this.$route.query.content);
    console.log(this.content);
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "WareHouseCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断
    this.listAxios();
  },
  methods: {
    // 添加仓库
    onSubmit() {
      let isDamaged = 0;
      if (this.content.damagedName == this.formInline.name) {
        isDamaged = 1;
      }
      if (this.formInline.name == "") {
        this.$message.warning("选择仓库不能为空");
      } else if (this.formInline.location == "") {
        this.$message.warning("库位不能为空");
      } else {
        this.axios
          .post(
            "/order/storageLocation/add",
            {
              request: {
                createdBy: sessionStorage.getItem("user_id"),
                name: this.formInline.location,
                warId: this.content.id,
                warName: this.formInline.name,
                isDamaged: isDamaged,
              },
              version: "",
            },
            {
              header: {
                isloading: false,
              },
            }
          )
          .then((res) => {
            // console.log(res);
            if (res.code == 0) {
              this.$message.success("添加成功");
              this.listAxios("1");
              this.dialoVisible = false;
              this.formInline = {};
            } else {
              this.$message.error(res.message);
            }
          });
      }
    },
    // 库位列表
    listAxios() {
      this.axios
        .post("/order/storageLocation/list", {
          keyword: this.formInline1.name,
          pageNo: 1,
          pageSize: 20,
          warId: this.content.id,
        })
        .then((res) => {
          //   console.log(res);
          if (res.code == 0) {
            for (let i in res.data.list) {
              res.data.list[i].edit = false;
              res.data.list[i].name1 = res.data.list[i].name;
              res.data.list[i].warName1 = res.data.list[i].warName;
            }
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 修改仓位名
    fixHouser(row) {
      if(!row.name || !row.warName){
        this.$message.warning("不能为空");
        return;
      }
      this.axios
        .post(
          "/order/storageLocation/update",
          {
            request: {
              name: row.name,
              id: row.id,
              warName: row.warName,
              updatedBy: sessionStorage.getItem("user_id"),
            },
          },
          {
            header: {
              isloading: false,
            },
          }
        )
        .then((res) => {
          //   console.log(res);
          this.listAxios();
          if (res.code == 0) {
            this.$message.success("修改成功");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***删除数据对接***/
    delect(row, index) {
      this.$confirm("确定要删除该库位吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/order/storageLocation/delete?id=" + row.id)
            .then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.tableData.list.splice(index, 1);
                this.$message.success("删除成功");
                // this.listAxios("1");
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /***删除数据对接***/
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.content {
  padding: 10px;
  background: #fff;
  .el-button {
    padding: 6px 10px;
  }
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
/deep/.el-table__row .cell {
}
.optain {
  width: 100%;
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  p {
    display: inline-block;
    padding: 0 5px;
    img {
      width: 20px;
      height: 20px;
    }
  }
  p:hover {
    cursor: pointer;
  }
}
.dialog-footer {
  text-align: right;
}
</style>
